#ifndef STRING_H
#define STRING_H

#include "../head.h"

class String
{
public:
    // 此处定义为noexcept可以使vector在扩张capicity时候调用移动构造函数而不是拷贝构造 
    String(String &&other) noexcept : m_data(other.m_data) 
    {
        cout << "move constructor" << endl;
        other.m_data = nullptr;
    }

    String &operator=(String &&other)
    {
        if(this == &other)
            return *this;
        m_data = other.m_data;
        cout << "move operator=" << endl;
        other.m_data = nullptr;
        return *this;
    }

    char &operator[](int pos)
    {
        cout << "non_const operator []" << endl;
        return 
        const_cast<char &>
        (
            static_cast<const String &>(*this)[pos]
        );
    }
    const char &operator[](int pos) const
    {
        // if(*(m_data+pos) == '\0')
        // {
        //     cout << "over range" << endl;
        //     exit(0);
        // }
        cout << "const operator []" << endl;
        return m_data[pos];
    }

    String(const char *cstr = nullptr)
    {
        cout << "cstr copy constructor" << endl;
        if(cstr == nullptr)
        {
            m_data = new char[1];
            *m_data = '\0';
        }
        else
        {
            m_data = new char[strlen(cstr)+1];
            strcpy(m_data, cstr);
        }
    }

    ~String() {delete [] m_data;}

    String(const String &str)
    { 
        cout << "string copy constructor" << endl;
        m_data = new char[strlen(str.m_data)+1];
        strcpy(m_data, str.m_data);   
    }

    String &operator=(const String &str)
    {
        cout << "operator =" << endl;
        if(this == &str)
            return *this;
        delete [] m_data;
        m_data = new char[sizeof(str.m_data)+1];
        strcpy(m_data, str.m_data); 
        return *this;
    }

    String operator+(const string &other)
    {

        return String(strcat(m_data, other.c_str()));
    }

    char *get_cstr() const
    {
        return m_data;
    }

    static void *operator new(size_t size)
    {
        String *p = (String *)malloc(size);
        cout << "static void * Foo::operator new(size_t size),size = " << size<<" p = "<<p << endl;
        return p;
    }

    static void operator delete(void *pdead, size_t size)
    {
        cout << "static void operator delete(void * pdead, size_t size),pdead = "<<pdead<<" size = "<<size<<endl;
        free(pdead);
    }

    static void *operator new[](size_t size)
    {
        String *p = (String *)malloc(size);
        cout << "static void * Foo::operator new(size_t size),size = " << size<<" p = "<<p << endl;
        return p;
    }

    static void operator delete[](void *pdead, size_t size)
    {
        cout << "static void operator delete(void * pdead, size_t size),pdead = "<<pdead<<" size = "<<size<<endl;
        free(pdead);
    }
private:
    char *m_data;
};

// 重载<<运算符，非成员函数
inline ostream &
operator <<(ostream &os, const String &s)
{
    // 注意const对象只能调用const限定的函数类型
    return os << s.get_cstr();
}

#endif